package com.triones.biz.infra.controller.web.file;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.triones.biz.infra.entity.file.bo.FileConfigBO;
import com.triones.biz.infra.entity.file.vo.FileConfigVO;
import com.triones.biz.infra.service.file.FileConfigService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 管理后台 - 文件配置
 *
 * @author triones
 * 2023-09-19
 */
@Validated
@RestController
@RequestMapping("/infra/file/config")
public class FileConfigController extends BaseController {

    @Resource
    private FileConfigService fileConfigService;

    /**
     * 查询文件配置列表
     */
    @SaCheckPermission("infra:fileConfig:list")
    @GetMapping("/list")
    public TableDataInfo<FileConfigVO> list(FileConfigBO bo, PageQuery pageQuery) {
        return fileConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取文件配置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("infra:fileConfig:query")
    @GetMapping("/{id}")
    public R<FileConfigVO> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(fileConfigService.queryById(id));
    }

    /**
     * 新增文件配置
     */
    @SaCheckPermission("infra:fileConfig:add")
    @Log(title = "新增文件配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FileConfigBO bo) {
        return toAjax(fileConfigService.insertByBo(bo));
    }

    /**
     * 修改文件配置
     */
    @SaCheckPermission("infra:fileConfig:edit")
    @Log(title = "修改文件配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FileConfigBO bo) {
        return toAjax(fileConfigService.updateByBo(bo));
    }

    /**
     * 修改文件配置
     */
    @SaCheckPermission("infra:fileConfig:edit")
    @Log(title = "更新文件配置为 Master", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/{id}/update-master")
    public R<Void> updateFileConfigMaster(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        fileConfigService.updateFileConfigMaster(id);
        return R.ok();
    }

    /**
     * 删除文件配置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("infra:fileConfig:remove")
    @Log(title = "删除文件配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(fileConfigService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
